<?php
namespace App\Http\Controllers\ApiAdmin;

use App\DBs\StockDB;
use Illuminate\Http\Request;

class StockController extends BaseController
{
    /**
     * 库
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new StockDB();
    }

    /**
     * 视频列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $page = $request->post('page',0);
        $size = $request->post('size',$this->limit);
        //梳理数据
        $resDb = $this->db->getAdminIndex($page,$size);
//        return $resDb;
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-3);
        }
        $dataArr = $resDb['data'];
        $total = $resDb['total'];
        return $this->toAdminJson(0, $dataArr, $total);
    }

    /**
     * 一个视频
     */
    public function getOne(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $vid = $request->post('vid',0);
        //梳理数据
        $resDb = $this->db->getAdminOne($vid);
//        return $resDb;
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-3);
        }
        $dataArr = $resDb['data'];
        return $this->toAdminJson(0, $dataArr);
    }

    /**
     * 新增表单
     */
    public function setFormAdd(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $fromType = $request->post('vtype',0);
        $fileTitle = $request->post('vtitle',0);
        $fileImg = $request->post('vimg',0);
        $fileLink = $request->post('vlink',0);
        $types = $this->db->getTypes();
        if (!array_key_exists($fromType, $types) || $fileTitle || !$fileLink) {
            return $this->toAdminJson(-2);
        }
        //判断重复
        $resDb2 = $this->db->getAdminOneByLink($fileLink);
        if ($resDb2['code']!=-1) {
            return $this->toAdminJson(-4);
        }
        //梳理数据
        $dataArr = array(
            'fromType' => $fromType,
            'fileTitle' => $fileTitle,
            'fileImg' => $fileImg,
            'fileLink' => $fileLink,
            'created_at' => time(),
        );
        $resDb = $this->db->setAdd($dataArr);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }

    /**
     * 更新表单
     */
    public function setFormUpdate(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $vid = $request->post('vid',0);
        $fromType = $request->post('vtype',0);
        $fileTitle = $request->post('vtitle',0);
        $fileImg = $request->post('vimg',0);
        $fileLink = $request->post('vlink',0);
        $types = $this->db->getTypes();
        if ($vid || !array_key_exists($fromType, $types)
            || $fileTitle || !$fileLink) {
            return $this->toAdminJson(-2);
        }
        //看看有没有
        $resDb3 = $this->db->getAdminOne($vid);
        if ($resDb3['code']!=0) {
            return $this->toAdminJson(-3);
        }
        //判断重复
        $resDb2 = $this->db->getAdminOneByLink($fileLink);
        if ($resDb2['code']==0 && $resDb2['data']['videoId']!=$vid) {
            return $this->toAdminJson(-4);
        }
        //梳理数据
        $dataArr = array(
            'fromType' => $fromType,
            'fileTitle' => $fileTitle,
            'fileImg' => $fileImg,
            'fileLink' => $fileLink,
            'updated_at' => time(),
        );
        $resDb = $this->db->setUpdate($dataArr);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }

    /**
     * 新增Json
     */
    public function setJsonAdd(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取数据
        $json = $request->post('json','');
        if (!$json) {
            return $this->toAdminJson(-2);
        }
        $jsons = explode("\n", $json);
        //判断重复
        $resDb2 = $this->db->getAdminOneByLink($jsons[4]);
        if ($resDb2['code']!=-1) {
            return $this->toAdminJson(-4);
        }
        $vtype = 0; //来源分类
        if (strpos($jsons[1],'kuaishou')) { //快手APP
            $vtype = 1;
        } else if (strpos($jsons[1],'shua')) { //刷宝APP
            $vtype = 3;
        } else if (strpos($jsons[1],'douyin')) { //抖音APP
            $vtype = 2;
        } else {
            return $this->toAdminJson(-2);
        }
        if (!$vtype) {
            return $this->toAdminJson(-2);
        }
        $items = array(
            'fromType' => $vtype,
            'fileTitle' => $jsons[2],
            'fileImg' => $jsons[3],
            'fileLink' => $jsons[4],
            'created_at' => time(),
        );
//        return $items;
        $resDb = $this->db->setAdd($dataArr);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }

    /**
     * 删除一个
     */
    public function setDel(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $vid = $request->post('vid',0);
        //梳理数据
        $resDb = $this->db->setAdminDel($vid, 1);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }

    /**
     * 删除一个
     */
    public function setRedo(Request $request)
    {
        //接口权限值
        if ($request->adminAccess != $this->adminAccess) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $vid = $request->post('vid',0);
        //梳理数据
        $resDb = $this->db->setAdminDel($vid, 0);
        if ($resDb['code']!=0) {
            return $this->toAdminJson(-500);
        }
        return $this->toAdminJson(0);
    }
}